app.controller('globalConfigCtrl', function ($scope, $location, ajax, toaster, logger, fileUpload, cos) {

	$scope.list = [];

	$scope.flag = false;

	$scope.uploading = false;

	//查询
	$scope.reload = function () {
		var promise = ajax.call(new Req.GET({
			resource: 'config/list'
		}));
		promise.then(function (result) {
			$scope.configlist = result.result;
			for (var i = 0; i < $scope.configlist.length; i++) {
				var key = $scope.configlist[i].key;
				if (key == "system.pwd.default") {
					$scope.list.push($scope.configlist[i]);
				}
				var sp = key.split(".");
				if (sp[0] != "system") {
					$scope.list.push($scope.configlist[i]);
				} else if (key == "system.config.logo") {
					$scope.logo = $scope.configlist[i];
				} else if (key == "system.config.userInform") {
					$scope.userInform = $scope.configlist[i];
				} else if (key == "system.config.aboutUs") {
					$scope.aboutUs = $scope.configlist[i];
				} else if (key == "system.config.versions") {
					$scope.versions = $scope.configlist[i];
				} else if (key == "system.config.help") {
					$scope.help = $scope.configlist[i];
				} else if (key == "system.customer.protocol") {
					$scope.customer = $scope.configlist[i];
				} else if (key == "system.upload.apk") {
					$scope.apk = $scope.configlist[i];
				} else if (key == "system.config.phone") {
					$scope.phone = $scope.configlist[i];
				} else if (key == "system.app.driver") {
					$scope.driver = $scope.configlist[i];
				} else if (key == "system.config.dversions") {
					$scope.driversions = $scope.configlist[i];
				} else if (key == "system.flash.order") {
					$scope.flashOrder = $scope.configlist[i];
					if ($scope.flashOrder.value == "true") {
						$scope.flag = true;
					}
				} else if (key == "system.ios.passenger") {
					$scope.iosUrl = $scope.configlist[i];
				} else if (key == "system.ios.version") {
					$scope.iosVersion = $scope.configlist[i];
				}
			}
		}, function () { });
	};
	$scope.reload();

	var inserter = new Req.POST({ resource: $scope.resource });

	//更新
	$scope.update = function (config) {
		inserter.fields = config;
		inserter.id = config.id;
		inserter.resource = 'config/update';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '修改信息成功。');
			} else {
				toaster.pop('error', '错误', '修改信息失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '修改信息失败！');
		});
	};

	$scope.sx = function () {
		$scope.list = [];
		ajax.call(new Req.GET({
			resource: 'config/list'
		})).then(function (result) {
			$scope.configlist = result.result;
			for (var i = 0; i < $scope.configlist.length; i++) {
				var key = $scope.configlist[i].key;
				if (key == "system.pwd.default") {
					$scope.list.push($scope.configlist[i]);
				}
				var sp = key.split(".");
				if (sp[0] != "system") {
					$scope.list.push($scope.configlist[i]);
				}
			}

		}, function () { });
	};

	//恢复默认值
	$scope.reset = function (config) {
		var key = config.key;
		inserter.resource = 'config/reset' + "?key=" + key;
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '恢复默认值成功。');
				$scope.sx();
			} else {
				toaster.pop('error', '错误', '恢复默认失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '恢复默认失败！');
		});
	};

	//上传logo
	$scope.upload = function (logo) {
		if ($scope.fileToUpload != undefined) {
			var uploadUrl = cos.host() + cos.img() + '?attributeName=logo';
			fileUpload.upload($scope.fileToUpload, uploadUrl, null,
				function (result) {
					if (result.code == 200) {
						logo.value = result.filePath;
						inserter.fields = logo;
						inserter.id = logo.id;
						inserter.resource = 'config/update';
						ajax.call(inserter).then(function (result) {
							if (result.code == 200) {
								toaster.pop('success', '成功', '修改信息成功。');
							} else {
								toaster.pop('error', '错误', '修改信息失败！');
							}
						}, function () {
							toaster.pop('error', '错误', '修改信息失败！');
						});
					} else {
						toaster.pop('error', '错误', result.message);
					}
					$scope.uploading = false;
				}, function () {
					toaster.pop('error', '错误', '文件上传失败！');
					$scope.uploading = false;
				}
			);
		} else {
			toaster.pop('warning', '提示', '请上传一张图片！');
			$scope.submitting = false;
			return;
		}
	};

	//上传apk
	$scope.uploadAPK = function (apk) {
		var count = 0;
		if ($scope.fileToUpload != undefined) {
			var attributeName;
			if (apk.key == "system.app.driver") {
				attributeName = "?attributeName=driver";
			} else if (apk.key == "system.upload.apk") {
				attributeName = "?attributeName=96513";
			}
			$scope.uploading = true;
			var uploadUrl = cos.host() + cos.apk() + attributeName;
			fileUpload.upload($scope.fileToUpload, uploadUrl, null,
				function (result) {
					if (result.code == 200) {
						apk.value = result.filePath;
						count = 1;
						toaster.pop('success', '成功', '上传成功，请确认下载地址并保存。');
					} else {
						toaster.pop('error', '错误', '文件上传失败！');
					}
					$scope.uploading = false;
				}, function () {
					toaster.pop('error', '错误', '文件上传失败！');
					$scope.uploading = false;
				}

			);
			if (count == 0) {
				toaster.pop('warning', '提示', '正在上传中，请不要关闭此页面！');
			}
		} else {
			toaster.pop('warning', '提示', '请上传一个apk文件！');
			$scope.submitting = false;
			return;
		}
	};

	$scope.saveAPK = function (apk) {
		inserter.fields = apk;
		inserter.id = apk.id;
		inserter.resource = 'config/update';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '修改信息成功。');
			} else {
				toaster.pop('error', '错误', '修改信息失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '修改信息失败！');
		});
	};

	$scope.onoff = function (flashOrder) {
		if (flashOrder.value === "true") {
			flashOrder.value = "false";
			$scope.flag = false;
		} else {
			flashOrder.value = "true";
			$scope.flag = true;
		}
		inserter.fields = flashOrder;
		inserter.id = flashOrder.id;
		inserter.resource = 'config/update';
		ajax.call(inserter).then(function (result) {
			if (result.code == 200) {
				toaster.pop('success', '成功', '更改扫码下单方式成功。');
			} else {
				toaster.pop('error', '错误', '更改扫码下单方式失败！');
			}
		}, function () {
			toaster.pop('error', '错误', '更改扫码下单方式失败！');
		});
	};

});
